﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HumanResorcesProj.BLL;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;



namespace HumanResorcesProj.GUI.Extensions
{

    
    public partial class ChildAddNew : System.Web.UI.Page
    {
 
        bool idValidator=false;
        ChildrenBLL BLL = new ChildrenBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = SearchEmplyee.Curr.CurrEmpName + " " + SearchEmplyee.Curr.CurrEmpLastName;
            Label2.Text = SearchEmplyee.Curr.CurrEmpID;
             
        }
      
      
        protected void Button3_Click(object sender, EventArgs e)
        {
            string EmpId = Label2.Text;
            string childId = TextBox4.Text;
            string childName = TextBox3.Text;
            string ChildBirthDate = TextBox5.Text;
            string childGender = DropDownList1.SelectedValue;

            if (idValidator)
            {
                int numRow = BLL.InserNewChild(EmpId, childId, childName, ChildBirthDate, childGender);
             //   if (numRow == 0) MessageBox.Show("לא בוצעו שינויים", "הודעת מערכת", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
             //   if (numRow == 1) MessageBox.Show("הנתון התווסף בהצלחה", "הודעת מערכת", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (numRow == 0) MessageBox("לא בוצעו שינויים");
                if (numRow == 1) MessageBox("הנתון התווסף בהצלחה");
                
                childNumCount();
               
            }
            

            

      }

        private void MessageBox(string msg)
        {
            System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void MsgBox(string sMessage)
        {
            string msg = "&lt;script language=\"javascript\"&gt;";
            msg += "alert('" + sMessage + "');";
            msg += "&lt;/script>";
            Response.Write(msg);
        }


        protected void Button4_Click(object sender, EventArgs e)
        {


            Response.Redirect("ChildAddNew.aspx");

         /*   string EmpId = Label2.Text;
            string childId = TextBox4.Text;
            string childName = TextBox3.Text;
            string ChildBirthDate = TextBox5.Text;
            string childGender = DropDownList1.SelectedValue;
            if (idValidator)
            {
                int numRow = BLL.InserNewChild(EmpId, childId, childName, ChildBirthDate, childGender);
              //  if (numRow == 0) MessageBox.Show("לא בוצעו שינויים", "הודעת מערכת", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
              //  if (numRow == 1) MessageBox.Show("הנתון התווסף בהצלחה", "הודעת מערכת", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (numRow == 0) MsgBox("00");
                if (numRow == 1) MsgBox("11");


                childNumCount();
                //*Close The Current Window 
                RegisterStartupScript("load", "<script type=\"text/javascript\">\n" + "self.close();\n" + "<" + "/script>");
            }// end if (idValidator)
          */

        }//end protected void Button4_Click(object sender, EventArgs e)

        public void childNumCount()
        {
            string  ParentName =  SearchEmplyee.Curr.CurrEmpID;
           
            int countRows = BLL.GetByName(ParentName).Rows.Count;
        
            Emp emp = new Emp();
            emp.UpdateChildNum(ParentName,countRows);
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value != "")
            {

                int[] arr = new int[9];
                int IDTest = Convert.ToInt32(args.Value);

                for (int i = 8; i >= 0; i--)
                {
                    arr[i] = IDTest % 10;
                    IDTest = IDTest / 10;
                }

                int[] arr2 = new int[9];
                int Sum = 0;
                int temp;
                for (int i = 0; i < 9; i++)
                {
                    if ((i % 2) == 1)
                    {
                        temp = 2 * arr[i];
                        if (temp <= 9)
                            arr2[i] = temp;
                        else switch (temp)
                            {
                                case 10: arr2[i] = 1;
                                    break;
                                case 12: arr2[i] = 3;
                                    break;
                                case 14: arr2[i] = 5;
                                    break;
                                case 16: arr2[i] = 7;
                                    break;
                                case 18: arr2[i] = 9;
                                    break;
                            }
                    }
                    else arr2[i] = arr[i];
                    Sum = Sum + arr2[i];

                }

                if (Sum % 10 == 0)
                    idValidator = args.IsValid = true;
                else idValidator = args.IsValid = false;
            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value == "")
                idValidator=args.IsValid = false;
            else idValidator=args.IsValid = true;
        }

    }
}
